using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CallFlowDesigner
{

    public partial class InboundMap : Form
    {
        public enum Result
        {
            NotSet,
            Cancel,
            Upload
        }

        public InboundMap()
        {
            InitializeComponent();
        }

        public CallFlowDesigner cf;

        private Result mapResult = Result.NotSet;
        public Result MapResult
        {
            get { return mapResult; }
        }


        public string SipAddress
        {
            get { return sipAddress.Text; }
        }

        public string FileName
        {
            get { return (string)lstXmlFiles.SelectedItem; }
        }


        private void UploadMap_Click(object sender, EventArgs e)
        {
            //string result = new BT.Sdk.InboundCallFlowCapability.InboundCallFlowManager().AddInboundCallMapping(sipAddress.Text, (string)lstXmlFiles.SelectedItem);
            mapResult = InboundMap.Result.Upload;
            this.Close();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            mapResult = InboundMap.Result.Cancel;
            this.Close();
        }     

        private void InboundMap_Load(object sender, EventArgs e)
        {
            lstXmlFiles.Items.Clear();
            foreach (object o in cf.lstProvisionedFiles.Items)
            {
                DataRowView d = (DataRowView)o;
                string s = (string)d.Row["filename"];
                if (s.Split('.')[1].ToLower() =="xml")
                {
                    lstXmlFiles.Items.Add(s);
                }
            }
            if (lstXmlFiles.Items.Count == 0)
            {
                MessageBox.Show("Please upload an XML file to CallFlow first", "CallFlow", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                mapResult = InboundMap.Result.Cancel;
                this.Close();
            }
            else
            {
                lstXmlFiles.SelectedIndex = 0;
            }
            sipAddress.Items.Clear();
            foreach (object o in cf.lstInboundMapfiles.Items)
            {
                ListViewItem lvi =(ListViewItem)o;
                if (lvi.Text.StartsWith("unbound"))
                {
                    sipAddress.Items.Add(lvi.SubItems[1].Text);
                }
            }
            if (cf.lstInboundMapfiles.SelectedItems.Count > 0 && cf.lstInboundMapfiles.SelectedItems[0].Text.StartsWith("unbound"))
            {
                sipAddress.Text = cf.lstInboundMapfiles.SelectedItems[0].SubItems[1].Text;
            }



        }
    }
}